#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>

//#define SAMPLE_RATE 11025
#define SAMPLE_FORMAT AFMT_S16_NE 
#define CHANNELS 1

int open_audio_device (char *name, int mode, int fs)
{
	int tmp, fd;
	
	if ((fd = open (name, mode, 0)) == -1)
    {
      	perror (name);
      	exit (-1);
    }

	//Setup the device. Note that it's important to set the sample format, number of channels and sample rate exactly in this order. Some devices 		depend on the order.

	//Set the sample format

  	tmp = SAMPLE_FORMAT;		/* Native 16 bits */
  	if (ioctl (fd, SNDCTL_DSP_SETFMT, &tmp) == -1)
    {
      	perror ("SNDCTL_DSP_SETFMT");
      	exit (-1);
    }

  	if (tmp != AFMT_S16_NE)
    {
      	fprintf (stderr,"The device doesn't support the 16 bit sample format.\n");
      	exit (-1);
    }

	//Set the number of channels (mono)
	tmp = CHANNELS;
  	if (ioctl (fd, SNDCTL_DSP_CHANNELS, &tmp) == -1)
    {
      	perror ("SNDCTL_DSP_CHANNELS");
      	exit (-1);
    }
  	if (tmp != 1)
    {
      	fprintf (stderr, "The device doesn't support mono mode.\n");
      	exit (-1);
    }

	//Set the sample rate
  	if (ioctl (fd, SNDCTL_DSP_SPEED, &fs) == -1)
    {
      	perror ("SNDCTL_DSP_SPEED");
      	exit (-1);
    }
	//No need for error checking because we will automatically adjust the signal based on the actual sample rate. However most application must check 	the value of sample_rate and compare it to the requested rate.
	//Small differences between the rates (10% or less) are normal and the applications should usually tolerate them. However larger differences may 	cause annoying pitch problems (Mickey Mouse).

  	return fd;
}

int gravaoss 
(
	short *buffer, 
	short bps, // bits por amostra
    int fs, // frequencia de amostragem do sinal
    int maxTime // maximun audio recording time in seconds
)
{
  	short *buffer1; // temporary buffer (will be alocated for 200ms recording time)
  	int nSamples;  // number of samples in buffer
  	int i,l,k; // counters
  	int time;
  	long loops;
	int fd_in; 
  	char *name_in = "/dev/dsp";
	int bufferSize;
	
	// Opening audio device
	fd_in = open_audio_device (name_in, O_RDONLY,fs);
	
	
  	nSamples = (int)(0.2*fs); // 200ms
  	bufferSize = nSamples*sizeof(short); // each sample has 16 bits
   	buffer1 = malloc(bufferSize); 
    
    loops = (long)(maxTime * fs / nSamples);
    //loops = (long)(loops/2); // 2 buffers for recording, alternating in a circular way
	/*
   	printf("PERIOD_TIME = %d\n",PERIOD_TIME);
   	printf("SAMPLE_RATE=%d\n",SAMPLE_RATE);
   	printf("loops=%ld\n",loops);
   	printf("nSamples=%d\n",nSamples);
	printf("bufferSize=%d\n",bufferSize);
	*/
	
	k=0;
   	for (time=0;time<loops;time++)
   	{
		//First read a block of audio samples with proper error checking.
  		if ((l = read (fd_in, buffer1, bufferSize)) != -1)
  		{
  			for(i=0;i<nSamples;i++)
  				buffer[k++] = buffer1[i];
  		}
  		else
    	{
      		perror ("Audio read");
      		exit (-1);		/* Or return an error code */
    	}
	}
   	free(buffer1);
   	close(fd_in);
   	return k;
}
